package com.zhuss.core.processor;

import com.zhuss.bean.bo.core.company.StartCompanyBo;
import com.zhuss.bean.vo.core.company.DetailCompanyVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.CompanyDao;
import com.zhuss.core.db.entity.Company;
import com.zhuss.core.processor.cache.CompanyCache;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.StartStopEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author summer
 * @date 2020/12/28 17:31
 */
@Component("companyStart")
public class CompanyStart extends AbstractProcessor<Object, StartCompanyBo> implements Processor<Object, StartCompanyBo> {

    @Autowired
    @Qualifier("companyRead")
    private CompanyRead companyRead;

    @Autowired
    private CompanyDao companyDao;

    @Autowired
    @Qualifier("companyCache")
    private CompanyCache companyCache;

    @Override
    public void check(StartCompanyBo startCompanyBo) throws DzcxException {
        DetailCompanyVo companyVo = (DetailCompanyVo) companyRead.detail(startCompanyBo.getCompanyId());
        if (StartStopEnum.启用.sameKey(companyVo.getStatus())) {
            throw new DzcxException(ExceptionCode.当前数据不满足启用条件);
        }
    }

    @Override
    public Object process(StartCompanyBo param) throws DzcxException {
        Long companyId = param.getCompanyId();

        Company record = new Company();
        record.setCompanyId(companyId);
        record.setStatus(param.getStatus());

        record.setModifierId(param.getConcurrentUserId());
        record.setModifier(param.getConcurrentUserName());
        record.setModifyTime(new Date());
        companyDao.updateByPrimaryKeySelective(record);

        String name = ((DetailCompanyVo) companyRead.detail(companyId)).getName();

        param.setName(name);

        companyCache.clear(companyId);
        return null;
    }

    @Override
    public Object log(StartCompanyBo param) throws DzcxException {
        OpLog record = new OpLog();
        record.setMoudle(MoudleEnum.基础模块.key());
        record.setAction(LogActionEnum.公司启用.key());
        record.setTargetId(param.getConcurrentUserId());
        record.setTargetName(param.getName());
        record.setContent("参数: " + JsonUtil.toJson(param));
        record.setReason(param.getExceptionReason());
        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }
}
